{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('11.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>标签的属性</title>\n",
       "</head>\n",
       "<body>\n",
       "<div class=\"demo test\" id=\"divid\">\n",
       "<p class=\"cp border\">p1</p>\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "<a href=\"a2\">a2</a>\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "div = sp.div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div class=\"demo test\" id=\"divid\">\n",
       "<p class=\"cp border\">p1</p>\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "<a href=\"a2\">a2</a>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['demo', 'test'] divid\n"
     ]
    }
   ],
   "source": [
    "print(div['class'],div['id'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 新增属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "div['lio'] = 'lio-class'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div class=\"demo test\" id=\"divid\" lio=\"lio-class\">\n",
       "<p class=\"cp border\">p1</p>\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "<a href=\"a2\">a2</a>\n",
       "</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "div['list'] = 'a b c d e'.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div class=\"demo test\" id=\"divid\" lio=\"lio-class\" list=\"a b c d e\">\n",
       "<p class=\"cp border\">p1</p>\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "<a href=\"a2\">a2</a>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "div['list'].append('jsdhfksdfhjs')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div class=\"demo test\" id=\"divid\" lio=\"lio-class\" list=\"a b c d e jsdhfksdfhjs\">\n",
       "<p class=\"cp border\">p1</p>\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "<a href=\"a2\">a2</a>\n",
       "</div>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除属性 【删除关键字：del】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "del div['lio']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div class=\"demo test\" id=\"divid\" list=\"a b c d e jsdhfksdfhjs\">\n",
       "<p class=\"cp border\">p1</p>\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "<a href=\"a2\">a2</a>\n",
       "</div>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
